package com.corona.game;

import com.corona.Game;

import java.lang.reflect.Field;
import java.util.HashMap;

/* compiled from: Action.java */
/* loaded from: pokewilds.jar:com/pkmngen/game/SetStaticField.class */
class SetStaticField extends Action {
    private static HashMap<String, Field> sFieldCache = new HashMap<>();
    Class<?> clazz;
    String fieldName;
    Object value;

    public SetStaticField(String clazz, String fieldName, Object value, Action nextAction) throws ClassNotFoundException {
        super(new Object[0]);
        this.nextAction = nextAction;
        this.clazz = Class.forName(clazz);
        this.fieldName = fieldName;
        this.value = value;
    }

    @Override // com.corona.game.Action
    public void step(Game game) {
        try {
            getField(this.clazz, this.fieldName).set(this.clazz, this.value);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e2) {
            e2.printStackTrace();
        } catch (Throwable e3) {
            e3.printStackTrace();
        }
        game.actionStack.remove(this);
        game.insertAction(this.nextAction);
    }

    private static Field getField(Class<?> clazz, String fieldName) throws Throwable {
        String fieldFullName = genFieldFullName(clazz, fieldName);
        if (sFieldCache.containsKey(fieldFullName)) {
            return sFieldCache.get(fieldFullName);
        }
        Field field = null;
        try {
            field = clazz.getField(fieldName);
        } catch (NoSuchFieldException e) {
        }
        if (field == null) {
            try {
                field = clazz.getDeclaredField(fieldName);
                field.setAccessible(true);
            } catch (NoSuchFieldException e2) {
            }
        }
        if (field == null) {
            clazz = clazz.getSuperclass();
            while (clazz != Object.class) {
                try {
                    field = clazz.getDeclaredField(fieldName);
                    field.setAccessible(true);
                } catch (NoSuchFieldException e3) {
                }
                clazz = clazz.getSuperclass();
            }
        }
        if (field == null) {
            throw new Throwable("Can't get Field from Class " + clazz.getSimpleName() + ":" + fieldName);
        }
        sFieldCache.put(fieldFullName, field);
        return field;
    }

    private static String genFieldFullName(Class<?> clazz, String fieldName) {
        return clazz.getName() + ":" + fieldName;
    }
}
